//
//  PSTextView.m
//  ZAYY
//
//  Created by Vic on 2018/9/18.
//  Copyright © 2018年 Vic. All rights reserved.
//

#import "PSTextView.h"

@interface PSTextView ()<UITextViewDelegate>
{
    UILabel *showNumberLabel;
    UILabel *placeholderFeedbackLabel;
    
    NSInteger limitMaxF;
}
@end

@implementation PSTextView

- (instancetype)initWithFrame:(CGRect)frame leftText:(NSString *)leftText text:(NSString *)text textColor:(UIColor *)color placeholder:(NSString *)placeholder font:(NSInteger)font editable:(BOOL)edit limitMax:(NSInteger)limitMax target:(id)target showNumber:(BOOL)showNumber {
    self = [super initWithFrame:frame];
    if (self) {
        limitMaxF = limitMax;
        [self initUIWithFrame:frame leftText:leftText text:text textColor:color placeholder:placeholder font:font editable:edit target:target showNumber:showNumber];
    }
    return self;
}
- (void)initUIWithFrame:(CGRect)frame leftText:(NSString *)leftText text:(NSString *)text textColor:(UIColor *)color placeholder:(NSString *)placeholder font:(NSInteger)font editable:(BOOL)edit target:(id)target showNumber:(BOOL)showNumber {
    if (target) {
        self.delegate = target;
    }
    
    CGFloat leftLabelWidth = [UIToolsControl textWidth:leftText size:CGSizeMake(MAXFLOAT, 0) font:font]+5*Scale;
    UILabel *leftLabel = [UIToolsControl initLabelWithFrame:CGRectMake(0, 0, leftLabelWidth, 50*Scale) text:leftText textColor:THEMECOLOR_OF_TEXT_333333 font:font];
    [self addSubview:leftLabel];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(EndX(leftLabel), 0, frame.size.width-ViewWidth(leftLabel), self.frame.size.height-35*Scale)];
    self.textView.contentInset = UIEdgeInsetsMake(10*Scale, 0, 0, 0);
    self.textView.editable = edit;
    self.textView.delegate = self;
    if (font) {
        self.textView.font = Font(font);
    }
    if (color) {
        self.textView.textColor = color;
    }
    if (text) {
        self.textView.text = text;
    }
    [self addSubview:self.textView];
    
    CGFloat placeholderHeight = [UIToolsControl textHeight:placeholder size:CGSizeMake(ViewWidth(self.textView), MAXFLOAT) font:font]+1;
    placeholderFeedbackLabel = [UIToolsControl initLabelWithFrame:CGRectMake(5*Scale, 7*Scale, ViewWidth(self.textView)-10*Scale, MAX(placeholderHeight, 17*Scale)) text:placeholder textColor:THEMECOLOR_OF_TEXT_cccccc font:font];
    placeholderFeedbackLabel.numberOfLines = 2;
    placeholderFeedbackLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self.textView addSubview:placeholderFeedbackLabel];
    
    if (text) {
        placeholderFeedbackLabel.hidden = YES;
    }
    
    showNumberLabel = [UIToolsControl initLabelWithFrame:CGRectMake(0, EndY(self.textView)+10*Scale, self.frame.size.width, 25*Scale) text:[NSString stringWithFormat:@"0/%ld", (long)limitMaxF] textColor:THEMECOLOR_OF_TEXT_cccccc font:font];
    showNumberLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:showNumberLabel];
    showNumberLabel.hidden = !showNumber;
    if (text && text.length) {
        showNumberLabel.text = [NSString stringWithFormat:@"%lu/%ld", (unsigned long)text.length, (long)limitMaxF];
    }
    
//    [showNumberLabel borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
}
- (void)textViewDidChange:(UITextView *)textView {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psTextViewDidChange:)]) {
        [self.delegate psTextViewDidChange:textView];
    }
    if (textView.text.length > 0) {
        placeholderFeedbackLabel.hidden = YES;
    } else {
        placeholderFeedbackLabel.hidden = NO;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    if (selectedRange && pos) {
        return;
    }
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum >= limitMaxF) {
        NSString *s = [nsTextContent substringToIndex:limitMaxF];
        
        [textView setText:s];
    }
    if (existTextNum>limitMaxF) {
        return;
    }
    showNumberLabel.text = [NSString stringWithFormat:@"%ld/%ld", MAX(0, existTextNum), (long)limitMaxF];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
//        if (self.delegate && [self.delegate respondsToSelector:@selector(psTextViewShouldReturn:)]) {
//            [self.delegate psTextViewShouldReturn:textView];
//        }
//        [textView resignFirstResponder];
//        return NO;
    } else if (textView.text.length>limitMaxF) {
        return NO;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    if (selectedRange && pos) {
        NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
        NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
        NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
        if (offsetRange.location < limitMaxF) {
            return YES;
        } else {
            return NO;
        }
    }
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    NSInteger caninputlen = comcatstr.length;
    if (caninputlen >= 0) {
        return YES;
    } else {
        NSInteger len = text.length + caninputlen;
        NSRange rg = {0, MAX(len,0)};
        
        if (rg.length > 0) {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
            showNumberLabel.text = [NSString stringWithFormat:@"%d/%ld", 0, (long)limitMaxF];
        }
        return NO;
    }
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psTextViewShouldBeginEditing:)]) {
        [self.delegate psTextViewShouldBeginEditing:textView];
    }
    return YES;
}
- (void)setTextViewText:(NSString *)textViewText {
    self.textView.text = textViewText;
    if (self.textView.text.length > 0) {
        placeholderFeedbackLabel.hidden = YES;
    } else {
        placeholderFeedbackLabel.hidden = NO;
    }
}

@end
